<template>
  <div>
    <!-- <el-tabs v-model="activeKey" @tab-click="handleTab">
      <el-tab-pane label="鉴权明细" name="1"> </el-tab-pane>
      <el-tab-pane label="手续费明细" name="2"></el-tab-pane>
    </el-tabs> -->
    <el-card style="min-width: 1200px">
      <div class="tableSelectWrap">
        <el-form
          ref="balanceRecordForm"
          :model="commandForm"
          label-width="100px"
        >
          <el-row>
            <el-col :span="7">
              <el-form-item label="选择时间" prop="date">
                <el-date-picker
                  v-model="commandForm.date"
                  style="width: 100%"
                  :clearable="false"
                  :default-time="['00:00:00', '23:59:59']"
                  type="datetimerange"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="yyyy-MM-dd HH:mm:ss"
                >
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1">
              <el-form-item label="交易类型" prop="busType">
                <el-select
                  v-model="commandForm.busType"
                  clearable
                  style="width: 100%"
                  placeholder="请选择交易类型"
                >
                  <el-option
                    v-for="item of tradeType"
                    :key="item.id"
                    :label="item.label"
                    :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="7" :offset="1" style="min-width: 170px">
              <el-button
                class="width120"
                @click="resetForm('balanceRecordForm')"
                >重置</el-button
              >
              <el-button
                type="primary"
                class="width120"
                @click="
                  () => {
                    this.dataForm = JSON.parse(
                      JSON.stringify(this.commandForm)
                    );
                    this.handleQuery();
                  }
                "
                >查询</el-button
              >
            </el-col>
            <!-- <el-col :span="7" :offset="1" v-if="activeKey == '1'">
            <el-form-item label="提现状态" prop="status">
              <el-select
                v-model="commandForm.status"
                clearable
                style="width: 100%"
                placeholder="请选择提现状态"
              >
                <el-option
                  v-for="item of statusList"
                  :key="item.id"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col> -->
          </el-row>
        </el-form>
      </div>
    </el-card>
    <el-card
      class="textBox"
      style="min-width: 1200px; margin-bottom: -5px; margin-top: 15px"
    >
      <div class="setting">
        <span class="tipBox">
          <div>余额不足提醒</div>
          <div class="color" @click="openDia1">
            低于¥{{ authInfo.fmtNoticeThreshold || 0 }}
          </div>
          <div
            class="color"
            @click="openDia1"
            v-if="!authInfo.noticeTypes.includes(-1)"
          >
            {{
              authInfo.noticeTypes.includes(1) &&
              authInfo.noticeTypes.includes(0)
                ? "邮件短信"
                : authInfo.noticeTypes.includes(0)
                ? "短信"
                : "邮件"
            }}提醒
          </div>
          <div class="color" @click="openDia1" v-else>站内通知</div>
        </span>
        <el-switch v-model="noticeS" @change="openDia"></el-switch>
      </div>
    </el-card>
    <el-card style="margin-top: 15px; min-width: 1200px">
      <el-table
        :data="dataList"
        :height="fixedTableHeight"
        @selection-change="handleSelectionChange"
        ref="multipleTable"
      >
        <template slot="empty">
          <el-empty
            :image="require('@/assets/imgs/middleWork/k1.png')"
            :image-size="80"
            description="暂无相关数据"
          ></el-empty>
        </template>
        <el-table-column
          show-overflow-tooltip
          prop="gmtCreated"
          align="center"
          label="创建时间"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.gmtCreated ? scope.row.gmtCreated : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="orderNo"
          align="center"
          label="云尘付流水号"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.busNo ? scope.row.busNo : "-" }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          show-overflow-tooltip
          prop="info"
          align="center"
          label="业务类型"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtBusType ? scope.row.fmtBusType : "-" }}
            </div>
          </template>
        </el-table-column> -->
        <el-table-column
          show-overflow-tooltip
          prop="info"
          align="center"
          label="交易类型"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtBusType ? scope.row.fmtBusType : "-" }}
            </div>
          </template>
        </el-table-column>
        <el-table-column
          show-overflow-tooltip
          prop="fmtActualAmount"
          sortable
          align="center"
          label="金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
              :class="scope.row.busType == 0 ? 'succeed' : 'failed'"
            >
              {{ scope.row.busType == 0 ? "+" : "-" }}
              {{
                scope.row.fmtActualAmount ? scope.row.fmtActualAmount : "0.00"
              }}
            </div>
          </template>
        </el-table-column>
        <!-- <el-table-column
          show-overflow-tooltip
          prop="resetAmt"
          align="center"
          label="交易状态"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
              :class="`${
                scope.row.fmtStatus.includes('失败')
                  ? 'failed'
                  : scope.row.fmtStatus.includes('成功')
                  ? 'succeed'
                  : 'pedding'
              } `"
            >
              {{ scope.row.fmtStatus ? scope.row.fmtStatus : "-" }}
            </div>
          </template>
        </el-table-column> -->
        <el-table-column
          show-overflow-tooltip
          prop="resetAmt"
          sortable
          align="center"
          label="剩余金额/元"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.fmtResidueMoney ? scope.row.fmtResidueMoney : "-" }}
            </div>
          </template>
        </el-table-column>

        <!-- <el-table-column
          show-overflow-tooltip
          prop="resetAmt"
          align="center"
          label="备注"
        >
          <template slot-scope="scope">
            <div
              style="
                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
              "
            >
              {{ scope.row.remark ? scope.row.remark : "-" }}
            </div>
          </template>
        </el-table-column> -->
        <!-- <el-table-column
          show-overflow-tooltip
          prop="act"
          align="center"
          label="操作"
        >
          <template slot-scope="scope">
            <div v-if="activeKey == '1'">
              <el-button
                type="text"
                @click="handleDetails(scope.row)"
                v-if="scope.row.status !== 'processing'"
                >详情</el-button
              >
              <span v-else>
                <el-button type="text">同意</el-button>
                &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <el-button type="text">拒绝</el-button>
              </span>
            </div>
            <div v-else>
              <el-button
                type="text"
                @click="handleDetails(scope.row)"
                v-if="scope.row.status !== 'processing'"
                >详情</el-button
              >
            </div>
          </template>
        </el-table-column> -->
      </el-table>
    </el-card>
    <el-pagination
      layout="prev, pager, next, sizes, jumper"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :current-page="pageNum"
      :total="total"
      @current-change="pageCurrentChangeHandle"
      @size-change="pageSizeChangeHandle"
    >
    </el-pagination>
    <!-- <details-drawer :drawer.sync="drawer" :info="detailsInfo"></details-drawer> -->

    <!-- 余额不足弹窗 -->
    <el-dialog
      class="cardDialog"
      :visible.sync="chargeDia"
      :show-close="false"
      width="520px"
      top="30vh"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <div class="addTitle">
        <span>余额不足提醒</span>
      </div>
      <div class="tip">为不影响您的正常使用请务必合理设置</div>
      <el-form ref="form" :rules="rules" :model="form">
        <el-form-item
          label="最低金额"
          prop="fmtNoticeThreshold"
          label-position="top"
        >
          <el-select
            v-model="form.fmtNoticeThreshold"
            placeholder="请选择"
            class="width440"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通知" class="notice">
          <el-checkbox v-model="form.emailType" class="width440">
            <el-form-item prop="email">
              邮件&nbsp;
              <el-input
                v-model="form.email"
                maxlength="64"
                placeholder="请输入邮箱"
                class="width380"
              ></el-input>
            </el-form-item>
          </el-checkbox>
          <el-checkbox v-model="form.phoneType" class="width440">
            <el-form-item prop="mobilePhone">
              短信&nbsp;
              <el-input
                v-model="form.mobilePhone"
                placeholder="请输入手机号"
                class="width380"
              ></el-input>
            </el-form-item>
          </el-checkbox>
          <!-- <el-radio-group v-model="form.noticeType" class="width440">
            <el-radio :label="1">邮件 {{ authInfo.email || "" }}</el-radio>
            <el-radio :label="0" :disabled="authInfo.mobilePhone == null"
              >短信 {{ authInfo.mobilePhone || "" }}</el-radio
            >
          </el-radio-group> -->
        </el-form-item>
      </el-form>
      <!-- <div class="tip1" v-if="authInfo.mobilePhone == null">
        系统管理员账号还未绑定手机号，无法选择短信提醒。
      </div> -->
      <div slot="footer" class="dialog-footer">
        <el-button @click="cencelReset">取消</el-button>
        <el-button type="primary" @click="handleSubmit">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import mixin from "@/mixins/mixin";
import { isEmail, isMobile } from "@/utils/validate";

export default {
  name: "balanceDetails",
  mixins: [mixin],
  data() {
    const testPhone = (rule, value, callback) => {
      if (!value && this.form.phoneType) {
        callback(new Error("请输入手机号"));
      } else {
        if (isMobile(value) || !value) {
          callback();
        } else {
          callback(new Error("号码格式不正确"));
        }
      }
    };
    const checkEmail = (rule, value, callback) => {
      if (!value && this.form.emailType) {
        callback(new Error("请输入邮箱"));
      } else {
        if (isEmail(value) || !value) {
          callback();
        } else {
          callback(new Error("请输入有效的邮箱地址"));
        }
      }
    };
    return {
      tradeType: [
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "充值", value: 0 },
        { id: 3, label: "鉴权套餐购买", value: 4 },
        { id: 4, label: "用量超额费用", value: 2 },
        { id: 5, label: "升级补差", value: 3 },
      ],
      authInfo: {
        noticeTypes: [-1],
      },
      options: [
        // {
        //   value: "10",
        //   label: "10元",
        // },
        // {
        //   value: "50",
        //   label: "50元",
        // },
        // {
        //   value: "100",
        //   label: "100元",
        // },
      ],
      cities: ["上海", "北京", "广州", "深圳"],
      form: {
        fmtNoticeThreshold: "",
        noticeType: "1",
        emailType: true,
        phoneType: true,
        email: "",
        mobilePhone: "",
      },
      ruleForm: {},
      checked1Name: "邮件",
      checked2Name: "短信",
      rules: {
        fmtNoticeThreshold: [
          { required: true, message: "金额不能为空", trigger: "change" },
          // {
          //   validator: this.checkAmount,
          //   trigger: "change",
          // },
        ],
        email: [{ validator: checkEmail, trigger: "change" }],
        mobilePhone: [
          {
            pattern: /^(?!\s+).*(?<!\s)$/,
            message: "格式有误",
            trigger: "change",
          },
          {
            validator: testPhone,
            trigger: "change",
          },
        ],
        // noticeType: [
        //   { required: true, message: "不能为空", trigger: "change" },
        // ],
      },
      chargeDia: false,
      dataList: [],
      noticeS: false,
      multipleSelection: [],
      isPatch: false, //批量切换
      isTep: 1, //表格按钮切换
      activeKey: "1", //切换tab
      mixinViewModuleOptions: {
        getDataListURL: "/api/merchant/memberauth/searchByPage",
        createdIsNeed: false,
      },
      drawer: false,
      commandForm: {
        busType: "",
        date: [
          this.$moment().startOf("day").format("YYYY-MM-DD HH:mm:ss"),
          this.$moment().endOf("day").format("YYYY-MM-DD HH:mm:ss"),
        ],
        gmtCreatedBegin: this.$moment()
          .startOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
        gmtCreatedEnd: this.$moment()
          .endOf("day")
          .format("YYYY-MM-DD HH:mm:ss"),
      },
      detailsInfo: {},
      payChannel: [
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "微信", value: "wechat" },
        { id: 3, label: "支付宝", value: "ali" },
        { id: 4, label: "汇付", value: "ada" },
      ],
      statusList: [
        { id: 1, label: "所有", value: "" },
        { id: 2, label: "已完成", value: "success" },
        { id: 3, label: "处理中", value: "processing" },
        { id: 4, label: "提现失败", value: "fail" },
      ],
    };
  },
  created() {
    this.dataForm = JSON.parse(JSON.stringify(this.commandForm));
    this.handleQuery();
    //获取鉴权信息
    this.getAuthInfo();
    // let type = this.$route.query.type;
    //console.log(type);
    // this.activeKey = type.toString();
    // if (type !== undefined) {
    //   console.log(123123123)
    //   this.activeKey = type.toString();
    // }
  },
  methods: {
    getAuthInfo() {
      this.$http
        .get("/api/merchant/memberauth/getAuthAccount?business=balance")
        .then((res) => {
          if (res.code === "0000") {
            console.log(res.data);
            this.authInfo = res.data;
            this.noticeS = res.data.noticeTypes.includes(-1) ? false : true;
            this.options = res.data.noticeMoneys || [];
            // this.form.noticeType =
            //   res.data.noticeType == "-1" || res.data.noticeType == "1" ? 1 : 0;
            this.form.fmtNoticeThreshold =
              res.data.fmtNoticeThreshold == "0.00"
                ? ""
                : parseInt(res.data.fmtNoticeThreshold);
            // this.form.noticeTypes = res.data.noticeTypes;
            this.form.email = res.data.email;
            this.form.mobilePhone = res.data.mobilePhone;
            // console.log(res.data.noticeTypes.includes(0))
            this.form.emailType = res.data.noticeTypes.includes(1);
            this.form.phoneType = res.data.noticeTypes.includes(0);
            // console.log(this.form.fmtNoticeThreshold)
            // this.form.checked1 = res.data.noticeType == "1" ? true : false;
            // this.form.checked2 = res.data.noticeType == "0" ? true : false;
          }
        });
    },
    openDia1() {
      this.form.fmtNoticeThreshold = parseInt(this.authInfo.fmtNoticeThreshold);
      this.form.emailType = this.authInfo.noticeTypes.includes(1);
      this.form.phoneType = this.authInfo.noticeTypes.includes(0);
      this.form.email = this.authInfo.email;
      this.form.mobilePhone = this.authInfo.mobilePhone;
      this.chargeDia = true;
    },
    openDia(val) {
      console.log(val);
      if (val) {
        // this.form.fmtNoticeThreshold = "";
        // this.form.email = "";
        // this.form.mobilePhone = "";
        // this.openDia1();
        this.chargeDia = true;
      } else {
        //发起关闭提醒权限请求
        this.$confirm("此操作将关闭余额不足提醒, 是否继续?", "提示", {
          customClass: "confirm",
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          showClose: false,
          closeOnClickModal: false,
          closeOnPressEscape: false,
          type: "warning",
          center: true,
        })
          .then(() => {
            let param = {
              id: this.authInfo.id,
              noticeTypes: [-1],
              business: "balance",
              // fmtNoticeThreshold: this.authInfo.fmtNoticeThreshold,
            };
            console.log(param);
            this.$http
              .post("/api/merchant/memberauth/updateAuthAccount", param)
              .then((res) => {
                if (res.code === "0000") {
                  this.$message.success("关闭成功");
                  this.cencelReset();
                  this.getAuthInfo();
                }
              });
          })
          .catch(() => {
            this.noticeS = true;
            // this.$message({
            //   type: "info",
            //   message: "已取消删除",
            // });
          });
      }
    },
    noticeSele(value) {
      console.log(value);
      // console.log(idx)
      console.log(this.form.checked1);
      if (value == 1) {
        //邮箱提醒
        if (this.form.checked2) {
          this.form.checked2 = !this.form.checked2;
        }
      } else {
        if (this.form.checked1) {
          this.form.checked1 = !this.form.checked1;
        }
      }
      // let checkedCount = value.length;
      // this.checkAll = checkedCount === this.cities.length;
      // this.isIndeterminate =
      //   checkedCount > 0 && checkedCount < this.cities.length;
    },
    cencelReset() {
      if (this.authInfo.noticeTypes.includes(-1)) {
        this.noticeS = false;
      }

      this.chargeDia = false;
      this.$refs.form.resetFields(); //重置状态
    },
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let newNoticeTypes = [];
          if (this.form.emailType) {
            newNoticeTypes.push(1);
          }
          if (this.form.phoneType) {
            newNoticeTypes.push(0);
          }
          if (!this.form.emailType && !this.form.phoneType) {
            newNoticeTypes.push(-1);
          }

          let param = {
            ...this.form,
            id: this.authInfo.id,
            noticeTypes: newNoticeTypes,
            business: "balance",
          };
          console.log(param);
          this.$http
            .post("/api/merchant/memberauth/updateAuthAccount", param)
            .then((res) => {
              if (res.code === "0000") {
                this.$message.success("设置成功");
                this.cencelReset();
                this.getAuthInfo();
              }
            });
        } else {
          return false;
        }
      });
    },
    handleTab() {
      this.pageSize = 10;
      this.resetForm("balanceRecordForm");
      if (this.activeKey == "1") {
        //获取鉴权信息
        this.getAuthInfo();
      }
      this.handleQuery();
    },
    handleClick() {
      this.isPatch = true;
      this.isTep = 2;
    },
    resetClick() {
      this.isPatch = false;
      this.multipleSelection = [];
      this.$refs.multipleTable.clearSelection();
      this.isTep = 1;
      //console.log(this.multipleSelection);
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      //console.log(this.multipleSelection);
    },
    // handleClick(tab, event) {
    //   console.log(tab, event);
    // },
    handleDetails(row) {
      this.drawer = true;
      this.detailsInfo = {
        提现时间: row.gmtCreated,
        商户: row.memberName,
      };
    },
    handleQuery() {
      this.pageNum = 1;
      if (this.dataForm.date && this.dataForm.date.length) {
        this.dataForm.gmtCreatedBegin = this.dataForm.date[0];
        this.dataForm.gmtCreatedEnd = this.dataForm.date[1];
      } else {
        this.dataForm.gmtCreatedBegin = "";
        this.dataForm.gmtCreatedEnd = "";
      }
      if (this.activeKey == "1") {
        this.getAuthList();
      } else {
        this.dataList = [];
        this.total = 0;
        this.noticeS = false;
        this.authInfo.noticeType = "-1";
      }
      // this.getDataList();
    },
    getAuthList() {
      this.$http
        .post("/api/merchant/memberauth/searchByPage", {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          params: {
            ...this.dataForm,
          },
          ...this.bodyForm,
        })
        .then((res) => {
          if (res.code == "0000") {
            console.log(res.data);
            this.dataList = res.data.list;
            this.total = res.data.total;
          }
        })
        .catch(() => {
          // this.dataListLoading = false
        });
    },
  },
};
</script>

<style scoped lang="scss">
.failed {
  color: #f42b2b;
}
.succeed {
  color: #07a555;
}
.pedding {
  color: #f69c1f;
}

.notice {
  :deep(.el-radio) {
    display: block;
    margin: 10px 0;
  }
}
.width440 {
  width: 440px;
}
.width380 {
  width: 380px;
}
.cardDialog {
  .addTitle {
    font-size: 20px;
    font-family: PingFangSC-Medium, PingFang SC;
    font-weight: bold;
    color: rgba(0, 0, 0, 0.85);
  }
  .tip {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: rgba(0, 0, 0, 0.65);
    margin-top: 12px;
    margin-bottom: 30px;
    span {
      color: #03bfc6;
    }
  }
  .tip1 {
    font-size: 12px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #869daa;
    // line-height: 17px;
  }
  :deep(.el-form-item--small.el-form-item:nth-of-type(2)) {
    margin-bottom: 0;
  }
  :deep(.el-dialog) {
    border-radius: 16px;
  }
  :deep(.el-dialog__body) {
    padding: 10px 40px;
    padding-bottom: 60px;
  }
  :deep(.el-button--small) {
    width: 74px;
  }
  :deep(.el-input__inner) {
    padding-left: 12px;
  }
  :deep(.el-form-item__content) {
    position: relative;
  }
  .form-img {
    width: 22px;
    border-radius: 22px;
    position: absolute;
    top: 5px;
    left: 10px;
    z-index: 1;
  }
}
.setting {
  display: flex;
  justify-content: space-between;
  .tipBox {
    font-size: 14px;
    font-family: PingFangSC-Regular, PingFang SC;
    font-weight: 400;
    color: #666666;
    display: flex;
    div {
      margin: 0 10px;
      cursor: pointer;
      &.color {
        color: #03bfc6;
      }
    }
  }
}

.pedding {
  color: #ee8917;
}
.switch {
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
.ml {
  margin-left: 50px;
}
.search {
  position: absolute;
  right: 0;
  top: -164px;
}
</style>
